/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.jtech.jdf.mimetool.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import org.cip4.jdflib.util.MimeUtil;
import org.jdesktop.application.AbstractBean;
import org.jdesktop.observablecollections.ObservableCollections;
import org.jdesktop.observablecollections.ObservableList;

/**
 *
 * @author awxva
 */
public class MultipartBean extends AbstractBean {

    private final ObservableList<BodyPartBean> bodyPartBeans;
    private Multipart multipart = null;
    private String encoding = "base64";

    public MultipartBean() {
        super();
        bodyPartBeans = ObservableCollections.observableList(new ArrayList<BodyPartBean>());
    }

    public void setMultipart(Multipart multipart) {
        this.multipart = multipart;
        final int partCount;
        try {
            partCount = multipart.getCount();
        } catch (MessagingException ex) {
            Logger.getLogger(MultipartBean.class.getName()).log(Level.SEVERE, "Could not determine number of parts in Multipart.", ex);
            return;
        }
        for (int i = 0; i < partCount; i++) {
            try {
                addBodyPart(multipart.getBodyPart(i));
            } catch (MessagingException ex) {
                Logger.getLogger(MultipartBean.class.getName()).log(Level.SEVERE, "Could not add part. Continueing anyway...", ex);
            }
        }
    }

    public Multipart getMultipart() {
        return multipart;
    }

    public void setEncoding(String encoding) {
        String oldValue = this.encoding;
        this.encoding = encoding;
        firePropertyChange("encoding", oldValue, getEncoding());
    }
    
    public String getEncoding() {
        return encoding;
    }
    
    public void setBodyParts(BodyPart[] bodyParts) {
        List<BodyPartBean> bodyPartBeans = new ArrayList<BodyPartBean>();
        for (BodyPart bodyPart : bodyParts) {
            bodyPartBeans.add(new BodyPartBean(bodyPart));
        }
        setBodyParts(bodyPartBeans);
    }

    public void setBodyParts(List<BodyPartBean> bodyPartBeans) {        
        bodyPartBeans.clear();
        bodyPartBeans.addAll(bodyPartBeans);
    }

    public void addBodyPart(BodyPart bodyPart, int index)
    {
        
    }
    
    private void addBodyPart(BodyPart bodyPart) {
        addBodyPart(new BodyPartBean(bodyPart));
    }

    private void addBodyPart(BodyPartBean bodyPartBean) {
        bodyPartBeans.add(bodyPartBean);
    }

    public ObservableList<BodyPartBean> getBodyParts() {
        return bodyPartBeans;
    }
    
    public BodyPartBean getJdfBodyPart()
    {
        for (BodyPartBean bodyPartBean : getBodyParts()) {
            if (bodyPartBean.getContentType().contains(MimeUtil.VND_JDF)) {
                return bodyPartBean;
            }
        }
        return null;
    }
    
    public BodyPartBean getJmfBodyPart()
    {
        for (BodyPartBean bodyPartBean : getBodyParts()) {
            if (bodyPartBean.getContentType().contains(MimeUtil.VND_JMF)) {
                return bodyPartBean;
            }
        }
        return null;
    }
}
